package io.fabdialogmorph;


import io.fabdialogmorph.helper.AnimationGroupEnd;
import io.fabdialogmorph.helper.AnimationHelper;
import io.fabdialogmorph.helper.ViewBean;
import io.fabdialogmorph.utils.ColorUtils;
import io.fabdialogmorph.utils.DensityUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;

/**
 * MainAbility
 *
 * @since 2021-06-17
 */
public class MainAbility extends Ability {
    private static final int NUM_2 = 2;
    Button btWindowClose;
    Text title;
    private Image bfFab;
    private DependentLayout dialog;
    private AnimationHelper animationHelper;
    private DirectionalLayout mRevealView = null;
    private Boolean isShowBoolean = true;
    private DependentLayout bfWindowFloat;
    private ComponentContainer.LayoutConfig matchParams;
    private DependentLayout dlMain;
    private Text textWindowTitle;
    private Text textWindowValue;
    private StackLayout slWindowAll;
    private ViewBean viewBean;
    private AnimatorGroup animatorGroup;
    private AnimatorValue animatorValue;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);
        setupSharedEelementTransitions();
    }

    /**
     * setupSharedEelementTransitions
     */
    public void setupSharedEelementTransitions() {
        title = (Text) findComponentById(ResourceTable.Id_text_title);
        bfWindowFloat = (DependentLayout) findComponentById(ResourceTable.Id_bf_window_float);
        textWindowTitle = (Text) findComponentById(ResourceTable.Id_text_window_title);
        textWindowValue = (Text) findComponentById(ResourceTable.Id_text_window_value);
        btWindowClose = (Button) findComponentById(ResourceTable.Id_bt_window_close);
        slWindowAll = (StackLayout) findComponentById(ResourceTable.Id_sl_window_all);
        dlMain = (DependentLayout) findComponentById(ResourceTable.Id_dl_main);
        bfFab = (Image) findComponentById(ResourceTable.Id_bf_fab);
        animationHelper = new AnimationHelper(getContext());
        viewBean = new ViewBean();
        textWindowTitle.setFont(Font.DEFAULT_BOLD);
        title.setFont(Font.DEFAULT_BOLD);
        float wx = DensityUtils.getDisplayWidth(getApplicationContext());
        float wy = DensityUtils.getDisplayHeight(getApplicationContext());
        float mx = (wx - bfWindowFloat.getComponentSize().getSizeX()) / NUM_2;
        float my = (wy - bfWindowFloat.getComponentSize().getSizeY())
                / NUM_2 - bfWindowFloat.getComponentSize().getSizeY() / NUM_2;
        float lx = wx - bfFab.getWidth();
        float ly = wy - (bfWindowFloat.getHeight() / NUM_2 + bfWindowFloat.getMarginBottom());
        viewBean.setMoveX(mx);
        viewBean.setMoveY(my);
        viewBean.setLocationX(lx);
        viewBean.setLocationY(ly);
        viewBean.setSlWindowAll(slWindowAll);
        viewBean.setDlMain(dlMain);
        isShowBoolean = true;
        bfWindowFloat.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                closeWindow();
            }
        });
        btWindowClose.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                closeWindow();
            }
        });
        bfFab.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                openWindow();
            }
        });
    }

    private void openWindow() {
        animatorGroup = new AnimatorGroup();
        animatorValue = new AnimatorValue();
        slWindowAll.setVisibility(Component.VISIBLE);
        bfWindowFloat.setVisibility(Component.VISIBLE);
        bfWindowFloat.setClickable(false);
        bfFab.setVisibility(Component.HIDE);
        setStackLayoutWindow(slWindowAll, isShowBoolean);
        textWindowTitle.setText(getString(ResourceTable.String_dialog_center_title));
        textWindowValue.setText(getString(ResourceTable.String_dialog_center_value));
        btWindowClose.setText(getString(ResourceTable.String_dialog_bt_close));
        animationHelper.isShowView(true);
        animationHelper.revealMenu(animatorGroup,viewBean, bfWindowFloat, bfFab, new AnimationGroupEnd() {
            @Override
            public void animationGroup() {
            }

            @Override
            public void animationGroupEnd() {
                bfWindowFloat.setClickable(true);
            }
        });
        if (isShowBoolean) {
            animationHelper.showOverlay(animatorValue, viewBean.getSlWindowAll(), new AnimationGroupEnd() {
                @Override
                public void animationGroup() {
                }

                @Override
                public void animationGroupEnd() {
                    bfWindowFloat.setClickable(true);
                }
            });
            isShowBoolean = false;
        }
    }

    private void closeWindow() {
        bfWindowFloat.setClickable(true);
        bfWindowFloat.setVisibility(Component.VISIBLE);
        viewBean.setWindowTitle(textWindowTitle);
        viewBean.setWindowValue(textWindowValue);
        viewBean.setWindowFloat(bfWindowFloat);
        animationHelper.isShowView(false);
        animationHelper.revealMenu(animatorGroup, viewBean, bfWindowFloat, bfFab, new AnimationGroupEnd() {
            @Override
            public void animationGroup() {
            }

            @Override
            public void animationGroupEnd() {
                setStackLayoutWindow(slWindowAll, false);
            }
        });
        if (!isShowBoolean) {
            animationHelper.showOverlay(animatorValue, viewBean.getSlWindowAll(), new AnimationGroupEnd() {
                @Override
                public void animationGroup() {
                }

                @Override
                public void animationGroupEnd() {
                    setStackLayoutWindow(slWindowAll, false);

                }
            });
        }
        isShowBoolean = true;
    }

    /**
     * setStackLayoutWindow
     *
     * @param stackLayout stackLayout
     * @param isShow      isShow
     */
    public void setStackLayoutWindow(StackLayout stackLayout, boolean isShow) {
        int intColor = Color.getIntColor("#AA000000");
        stackLayout.setAlpha(0);
        ShapeElement shapeElementOver = new ShapeElement();
        if (isShow) {
            stackLayout.setVisibility(Component.HIDE);
            shapeElementOver.setRgbColor(getRgbColor(intColor));
        } else {
            stackLayout.setVisibility(Component.HIDE);
            shapeElementOver.setRgbColor(new RgbColor(0, 0, 0, 0));
        }
        stackLayout.setBackground(shapeElementOver);

    }

    /**
     * getRgbColor
     *
     * @param color color
     * @return RgbColor
     */
    public RgbColor getRgbColor(int color) {
        return new RgbColor(ColorUtils.redInt(color),
                ColorUtils.greenInt(color), ColorUtils.blueInt(color), Color.alpha(color));
    }


}
